#import "NewUISetViewController.h"

@interface NewUISetViewController () <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) NSArray *sectionTitles;
@property (nonatomic, strong) NSArray *itemTitles;
@property (nonatomic, strong) NSArray *itemIcons;
@property (nonatomic, strong) NSArray *itemAccessoryTypes;
@property (nonatomic, strong) NSMutableArray *filteredItemTitles;
@property (nonatomic, strong) NSMutableArray *filteredItemIcons;
@property (nonatomic, strong) NSMutableArray *filteredItemAccessoryTypes;
@property (nonatomic, assign) BOOL isSearching;

@end

@implementation NewUISetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupData];
    [self setupUI];
}

- (void)setupData {
    self.title = @"设置";
    
    self.sectionTitles = @[@"通用", @"通知", @"隐私与安全性", @"辅助功能", @"关于"];
    
    self.itemTitles = @[
        @[@"通用"],
        @[@"通知"],
        @[@"隐私与安全性"],
        @[@"辅助功能"],
        @[@"关于"]
    ];
    
    self.itemIcons = @[
        @[@"gear"],
        @[@"bell"],
        @[@"lock"],
        @[@"accessibility"],
        @[@"info.circle"]
    ];
    
    self.itemAccessoryTypes = @[
        @[@"disclosure"],
        @[@"disclosure"],
        @[@"disclosure"],
        @[@"disclosure"],
        @[@"disclosure"]
    ];
    
    // 初始化过滤后的数组
    self.filteredItemTitles = [NSMutableArray arrayWithArray:self.itemTitles];
    self.filteredItemIcons = [NSMutableArray arrayWithArray:self.itemIcons];
    self.filteredItemAccessoryTypes = [NSMutableArray arrayWithArray:self.itemAccessoryTypes];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor systemGroupedBackgroundColor];
    
    // 搜索栏
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
    self.searchBar.delegate = self;
    self.searchBar.placeholder = @"搜索";
    self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    self.searchBar.backgroundColor = [UIColor systemGroupedBackgroundColor];
    [self.view addSubview:self.searchBar];
    
    // TableView
    CGFloat tableViewY = CGRectGetMaxY(self.searchBar.frame);
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, tableViewY, self.view.bounds.size.width, self.view.bounds.size.height - tableViewY) style:UITableViewStyleInsetGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor systemGroupedBackgroundColor];
    [self.view addSubview:self.tableView];
}

#pragma mark - UISearchBarDelegate

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if (searchText.length == 0) {
        self.isSearching = NO;
        self.filteredItemTitles = [NSMutableArray arrayWithArray:self.itemTitles];
        self.filteredItemIcons = [NSMutableArray arrayWithArray:self.itemIcons];
        self.filteredItemAccessoryTypes = [NSMutableArray arrayWithArray:self.itemAccessoryTypes];
    } else {
        self.isSearching = YES;
        [self filterContentForSearchText:searchText];
    }
    [self.tableView reloadData];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
}

- (void)filterContentForSearchText:(NSString *)searchText {
    NSMutableArray *filteredTitles = [NSMutableArray array];
    NSMutableArray *filteredIcons = [NSMutableArray array];
    NSMutableArray *filteredAccessoryTypes = [NSMutableArray array];
    
    for (NSInteger section = 0; section < self.itemTitles.count; section++) {
        NSArray *sectionTitles = self.itemTitles[section];
        NSArray *sectionIcons = self.itemIcons[section];
        NSArray *sectionAccessoryTypes = self.itemAccessoryTypes[section];
        
        NSMutableArray *filteredSectionTitles = [NSMutableArray array];
        NSMutableArray *filteredSectionIcons = [NSMutableArray array];
        NSMutableArray *filteredSectionAccessoryTypes = [NSMutableArray array];
        
        for (NSInteger row = 0; row < sectionTitles.count; row++) {
            NSString *title = sectionTitles[row];
            if ([title.lowercaseString containsString:searchText.lowercaseString]) {
                [filteredSectionTitles addObject:title];
                [filteredSectionIcons addObject:sectionIcons[row]];
                [filteredSectionAccessoryTypes addObject:sectionAccessoryTypes[row]];
            }
        }
        
        if (filteredSectionTitles.count > 0) {
            [filteredTitles addObject:filteredSectionTitles];
            [filteredIcons addObject:filteredSectionIcons];
            [filteredAccessoryTypes addObject:filteredSectionAccessoryTypes];
        }
    }
    
    self.filteredItemTitles = filteredTitles;
    self.filteredItemIcons = filteredIcons;
    self.filteredItemAccessoryTypes = filteredAccessoryTypes;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.isSearching ? self.filteredItemTitles.count : self.sectionTitles.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *titles = self.isSearching ? self.filteredItemTitles[section] : self.itemTitles[section];
    return titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    // 重置cell的所有属性
    cell.imageView.image = nil;
    cell.accessoryView = nil;
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.textLabel.text = nil;
    
    // 配置cell
    NSArray *titles = self.isSearching ? self.filteredItemTitles[indexPath.section] : self.itemTitles[indexPath.section];
    NSArray *icons = self.isSearching ? self.filteredItemIcons[indexPath.section] : self.itemIcons[indexPath.section];
    NSArray *accessoryTypes = self.isSearching ? self.filteredItemAccessoryTypes[indexPath.section] : self.itemAccessoryTypes[indexPath.section];
    
    cell.textLabel.text = titles[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    
    // 设置图标
    NSString *iconName = icons[indexPath.row];
    if (iconName.length > 0) {
        cell.imageView.image = [UIImage systemImageNamed:iconName];
        cell.imageView.tintColor = [UIColor systemBlueColor];
    }
    
    // 设置附加视图
    NSString *accessoryType = accessoryTypes[indexPath.row];
    if ([accessoryType isEqualToString:@"disclosure"]) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (self.isSearching) {
        return nil;
    }
    return self.sectionTitles[section];
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // 处理cell点击事件
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

@end 
